package com.ganbo.ms.pool.controller;

import com.ganbo.ms.pool.pojo.SnapshotExtend;
import com.ganbo.ms.pool.pool.SnapshotExtendPool;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
@Slf4j
public class TestController {

    @Autowired
    SnapshotExtendPool pool;


    @GetMapping("/poolTest")
    public String test() {
        SnapshotExtend snapshotExtend = null;
        try {
            snapshotExtend = pool.borrowObject();
            //省略业务代码...

            System.out.println("===============>" + snapshotExtend);
            log.info("BorrowedCount: {},ReturnedCount: {}, CreatedCount: {}, DestroyedCount: {}"
                    , pool.getBorrowedCount(), pool.getReturnedCount(), pool.getCreatedCount(), pool.getDestroyedCount());
            Thread.sleep(6000);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (snapshotExtend != null) {
                //最终归还对象到对象池
//                pool.returnObject(snapshotExtend);
                pool.myReturnObject(snapshotExtend);
                pool.myReturnObject(snapshotExtend);
                pool.myReturnObject(snapshotExtend);
                pool.myReturnObject(snapshotExtend);
            }
        }
        return "success";
    }

}

